package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "5")int pageSize){

        PageInfo<Role> pageInfo = roleService.findByPage(pageNum, pageSize, getLoginCompanyId());
        //存储到域对象
        request.setAttribute("pageInfo",pageInfo);
        return "system/role/role-list";
    }

    /**
     * url:/system/role/toAdd.do
     * 作用：进入添加角色页面
     * 参数：无
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        //查询所有角色
        String companyId=getLoginCompanyId();
        List<Role> roleList = roleService.findAll(companyId);
        //把角色的信息存储到域钟
        request.setAttribute("roleList",roleList);
        return "system/role/role-add";
    }

    /**
     * url:/system/role/edit.do
     * 作用：新增角色||更新角色
     * 参数：角色对象
     * 返回值：角色列表页面
     */
    @RequestMapping("/edit")
    public String edit(Role role){
        String companyId=getLoginCompanyId();
        String companyName=getLoginCompanyName();
        role.setCompanyId(companyId);
        role.setCompanyName(companyName);
        if (StringUtils.isEmpty(role.getId())){
            roleService.save(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }
    /**
     * /system/Role/toUpdate.do?id=${Role.id}
     * 作用：进入更新角色页面
     * 角色的id
     * 返回值：角色更新页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //1.根据id查询当前用户信息
        Role role = roleService.findById(id);
        //2. 查询所有角色信息
        String companyId= getLoginCompanyId();
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        request.setAttribute("roleList",roleList);
        request.setAttribute("role",role);
        return "system/role/role-update";
    }

    /**
     * /system/role/delete.do?id="+id
     * 作用：删除角色
     * 返回值：map对象的json字符串
     */
    @RequestMapping("/delete")
    public String delete(String id){
        roleService.delete(id);
      return "redirect:/system/role/list.do";
    }
    /**
     * /system/role/roleModule.do?roleid="+id;
     * 进入角色权限列表
     * 参数角色的id
     * 返回值：角色权限的列表也买你
     */
    @RequestMapping("/roleModule")
    public String roleModule(String roleid){
        //根据角色id查找出角色
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";

    }
    /**
     *  //4. 定义树结构数据
     *     var zNodes =[
     *         { id:1, pId:0, name:"saas管理", open:true},
     *         { id:11, pId:1, name:"企业管理"},
     *         { id:111, pId:1, name:"模块管理"}
     *     ];
     *  url:"/system/role/getTreeNodes.do",
     *  处理ajax请求，返回ztree需要的json格式数据
     */
    @RequestMapping("/getTreeNodes")
    @ResponseBody
    public List<Map<String,Object>>getTreeNodes(String roleId){
        //查找所有的权限
        List<Module> moduleList = moduleService.findAll();
        //查找角色相对应的权限
        List<String> roleModuleList = moduleService.findRoleModuleByRoleId(roleId);
        //定义一个集合
        ArrayList<Map<String, Object>> resultList = new ArrayList<>();
        //遍历所有的权限，每一个权限对象就是一个map对象
        for (Module module : moduleList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            if (roleModuleList.contains(module.getId())) {
                map.put("checked",true);
            }
            //把map添加到list集合中
            resultList.add(map);

        }
        return resultList;
    }

    /**
     * 方法的作用：更新保存角色拥有权限
     * /system/role/updateRoleModule.do"
     * 参数 roleid角色id moduleIds 模块id字符串
     * 返回：角色列表
     */
    @RequestMapping("/updateRoleModule")
    public String updateRoleModule(String roleid,String[] moduleIds){
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }
}
